package com.nestease.qa.dao.api

import org.apache.ibatis.annotations.Insert
import org.apache.ibatis.annotations.Param
import org.apache.ibatis.annotations.Select
import org.apache.ibatis.annotations.Update

/**
 * @author zoudongdong
 * @date 2018/2/20
 */
interface UserTeamDAO {
    @Insert("""
            INSERT INTO `LG_user_team` (`userId`, `teamId`,`status`)
            VALUES (#{userId},#{teamId},1)
            """)
    public void joinUserTeam(@Param("userId") String userId,
                             @Param("teamId") int teamId);

    @Select("""
            select userId
            from LG_user_team
            where teamId=#{teamId}
            and status=1
            """)
    public List<String> getJoinedUserIdList(@Param("teamId") int teamId);


    @Insert("""
            update LG_user_team
            set status=0
            where userId=#{userId}
            and teamId=#{teamId}
            """)
    public void leaveUserTeam(@Param("userId") String userId,
                              @Param("teamId") int teamId);

    @Select("""
            select count(*)
            from LG_user_team
            where userId=#{userId}
            and teamId=#{teamId}
            """)
    public int ifOnceJoined(@Param("userId") String userId,
                            @Param("teamId") int teamId);

    @Update("""
            update LG_user_team
            set status=#{status}
            where userId=#{userId}
            and teamId=#{teamId}
            """)
    public void updateUserTeam(@Param("userId") String userId,
                               @Param("teamId") int teamId,
                               @Param("status") int status);


    @Select("""
           SELECT ut.teamId
            from LG_team t,LG_user_team ut
            WHERE t.teamId = ut.teamId
            and t.status=1
            and ut.userId=#{userId};
            """)
    public List<Integer> getJoinedTeamIdList(String userId);
}